-- 运营活动: 宝物折扣


--[[
{
    cmd_id = 1, -- 命令id，用于批量停止、删除活动 
    act_id = 66, -- 活动id (66 宝物折扣)
    act_name = "宝物折扣", -- 活动名称
    act_desc = "参与团购！你们的购买数量只要达到指定数量即可领取奖励！", -- 活动描述
    beg_time = 1493262000,  -- 活动开始时间(unix timestamp)
    end_time = 1693267400,  -- 活动结束时间(unix timestamp)
    config = -- 配置数据
    {
        itemList =  --兑换物品列表
        {
            { 
                limitNum = 10, 
                needGoldNum = 800, 
                describe = "花费多少元宝购买礼包", 
                award = 
                { 
                    { type = 0, id = 563, count = 2, bind = 1,},
                },
            },
            { 
                limitNum = 10, 
                needGoldNum = 800, 
                describe = "花费多少元宝购买礼包", 
                award = 
                { 
                    { type = 0, id = 563, count = 2, bind = 1,},
                },
            },
            { 
                limitNum = 10, 
                needGoldNum = 800, 
                describe = "花费多少元宝购买礼包", 
                award = 
                { 
                    { type = 0, id = 563, count = 2, bind = 1,},
                },
            },
            { 
                limitNum = 10, 
                needGoldNum = 800, 
                describe = "花费多少元宝购买礼包", 
                award = 
                { 
                    { type = 0, id = 563, count = 2, bind = 1,},
                },
            },
            { 
                limitNum = 10, 
                needGoldNum = 800, 
                describe = "花费多少元宝购买礼包", 
                award = 
                { 
                    { type = 0, id = 563, count = 2, bind = 1,},
                },
            },
            { 
                limitNum = 10, 
                needGoldNum = 800, 
                describe = "花费多少元宝购买礼包", 
                award = 
                { 
                    { type = 0, id = 563, count = 2, bind = 1,},
                },
            },
            { 
                limitNum = 10, 
                needGoldNum = 800, 
                describe = "花费多少元宝购买礼包", 
                award = 
                { 
                    { type = 0, id = 563, count = 2, bind = 1,},
                },
            },
        },
    },
}
]]

-- 检查配置是否正确，正确则返回 0，否则返回错误码
function oper_act_66_check_cfg(cfg)
    if cfg == nil then return 20 end
    
    local config = cfg.config
    if config == nil then return 21 end
    
    if config.itemList == nil then return 23 end
    if type(config.itemList) ~= "table" then return 24 end
    for idx = 1, #config.itemList do
        local item = config.itemList[idx]
        if item == nil then return 27 end
        local dayRewards = item.award
        if dayRewards == nil then return 25 end
        if type(dayRewards) ~= "table" then return 26 end
        if not oper_act_check_one_award_cfg(dayRewards) then
            return 22
        end 
    end

    return 0
end

-- 安装活动，成功则返回 0，否则返回错误码
function oper_act_66_setup(cfg) 
    return 0
end

-- 卸载活动
function oper_act_66_unsetup(cfg)   
end

-- 活动开始
function oper_act_66_start(cfg)
    local ssVar = System.getStaticVar()
    ssVar.oper_act_66_start = 1
    ssVar.oper_act_66_cmd = cfg.cmd_id
    ssVar.oper_act_66_act = cfg.act_id

    local data = gRankMgr_GetValueByIndx(0, RANK_DEFINE_OPERATION_66, 1)

    if data then
        local myData = BaseFuc_unserialize(data)
        if myData and myData.cmd_id and myData.cmd_id == cfg.cmd_id and myData.act_id and myData.act_id == cfg.act_id and
            myData.time and cfg.beg_time <= myData.time and  cfg.end_time > myData.time then
            return
        end
    end

    gRankMgr_Clear(RANK_DEFINE_OPERATION_66)
end

-- 活动结束
function oper_act_66_end(cfg)
    local ssVar = System.getStaticVar()
    ssVar.oper_act_66_start = nil
    ssVar.oper_act_66_cmd = nil
    ssVar.oper_act_66_act = nil
end

-- 打包数据
function oper_act_66_packet_data(sysarg, cfg)

    local ssVar = System.getStaticVar()
    if not ssVar.oper_act_66_start then
        return
    end
    if not cfg then
        return
    end

    local config = cfg.config
    if not config then
        return
    end

    
    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_66, 1)
    local myData = BaseFuc_unserialize(data)

    if not myData or myData == "" or myData == 0 then
        myData = {}
    end

    local time = System.getSystemTime()
    if myData.mySelfTime and not System.isUnixTimeSameDay(myData.mySelfTime, time) then
        myData.myBuyLimit = {}
    end

    if not myData.myBuyLimit then
        myData.myBuyLimit = {}
    end

    local pack = oper_act_alloc_init_data_packet(sysarg, cfg.cmd_id, cfg.act_id)
    if (pack == nil) then
        return
    end

    local len = #config.itemList

    DataPack.writeByte(pack, len)
    for idx = 1, len do
        local item = config.itemList[idx]
        DataPack.writeByte(pack, idx)
        local num = item.limitNum - (myData.myBuyLimit[idx] or 0)
        DataPack.writeShort(pack, num)
    end
    DataPack.flush(pack)

    --DataPack.writeByte(netPack, 1)        -- 活动特有参数
end


-- 操作，返回操作结果，0为成功
function oper_act_66_do(sysarg, cfg, clientPkt)
    if cfg == nil then return end
    
    if cfg.state == nil or cfg.state ~= 1 then -- 不在活动中
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation001, ttFlyTip)
        return
    end
    
    local config = cfg.config
    if config == nil then -- 参数错误
        print("error, oper_act_66_do cfg.config confg error")
        return
    end

    local nIndex = DataPack.readByte( clientPkt )


    local actorid = Actor.getActorId(sysarg)
    local data = gRankMgr_GetValueById(actorid, RANK_DEFINE_OPERATION_66, 1)

    local myData = BaseFuc_unserialize(data)
    

    if not myData or myData == "" or myData == 0 then
        myData = {}
    end

    local time = System.getSystemTime()
    if myData.mySelfTime and not System.isUnixTimeSameDay(myData.mySelfTime, time) then
        myData.myBuyLimit = {}
    end

    if not myData.myBuyLimit then
        myData.myBuyLimit = {}
    end

    local result = 0    

    local item = config.itemList[nIndex]

    if not item then
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation015, ttFlyTip)
        return
    end

    if myData.myBuyLimit[nIndex] and item.limitNum <= myData.myBuyLimit[nIndex] then
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.Operation016, ttFlyTip)
        return
    end

    if not myData.myBuyLimit[nIndex] then
        myData.myBuyLimit[nIndex] = 0
    end

    if IsBagGridEnough(sysarg, item.award) then
        if Actor.getMoneyCount(sysarg, mtYuanbao) < item.needGoldNum then
            Actor.sendItemLack( sysarg, qatYuanbao, 0, item.needGoldNum )
            return
        end

        if not Actor.delMoney( sysarg,mtYuanbao,item.needGoldNum,LogIds.OperationLogId066,Lang.LogTips.Operation066) then
            return
        end


        myData.myBuyLimit[nIndex] = myData.myBuyLimit[nIndex] + 1
        GiveCommonAward(sysarg, item.award, LogIds.OperationLogId066, Lang.LogTips.Operation066)

        if not myData.cmd_id then
            myData.cmd_id = cfg.cmd_id
            myData.act_id = cfg.act_id
            myData.time = time
        end

        myData.mySelfTime = time

        local newData = BaseFuc_serialize(myData)

        gRankMgr_SetRankEx(actorid, RANK_DEFINE_OPERATION_66, 1, newData)


        local netPack = oper_act_alloc_do_result_packet(sysarg, cfg.cmd_id, cfg.act_id, result)
        DataPack.writeByte(netPack, nIndex)
        local lastNum = item.limitNum - myData.myBuyLimit[nIndex]
        DataPack.writeShort(netPack, lastNum)
        DataPack.flush(netPack)
    else
        Actor.sendTipmsg(sysarg,Lang.ScriptTips.COM0015, ttFlyTip)
        return
    end
end


--[[
-- 跨天
function oper_act_66_new_day_arrive(act_id)  
    
end
]]




